//
//  BrandViewController.m
//  xingfugangwan
//
//  Created by rainday on 14/12/28.
//  Copyright (c) 2014年 rainday. All rights reserved.
//

#import "BrandViewController.h"
#import <AFNetworking.h>
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "NetIP.h"

@interface BrandViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong) NSMutableArray *dataArray;
@property (nonatomic ,weak) IBOutlet UITableView *tableView;


- (IBAction)dimissVC:(id)sender;
@end


static NSString *reuseIdentify =@"Cell";

@implementation BrandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self performSelectorInBackground:@selector(downData) withObject:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)downData
{
    __weak NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id __weak token = [userDefaults objectForKey:@"token"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    
    
    @weakify(self)
    [manager POST:[[NetIP shareNetAddress] getBrandURLString] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        @strongify(self)
//                NSLog(@"%@",responseObject);
        NSDictionary *dic = responseObject;
        [self performSelectorOnMainThread:@selector(setDataArray:) withObject:[NSMutableArray arrayWithArray:[dic objectForKey:@"品牌"]] waitUntilDone:YES];
        
        
        
       
        
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    
    
}


- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [_tableView reloadData];
    

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentify];
    }
    
    if ([_dataArray count]>0) {
        cell.textLabel.text = _dataArray[indexPath.row];
    }
    
    return cell;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}


- (void)dimissVC:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_delegate respondsToSelector:@selector(hasSelectedBrand:)]) {
        [_delegate hasSelectedBrand:[_dataArray objectAtIndex:indexPath.row]];
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
}
@end
